package com.dodol.util;


import com.dodol.builder.TestCaseFactory;
import com.dodol.builder.TestStepFactory;
import com.dodol.common.BaseTestCase;
import com.dodol.common.TestStep;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 80736
 * @Date: 2018/9/2 12:09
 * @Description: excel工具类
 */
public class ExcelUtil {

    private static List<BaseTestCase> testCases;


//    /**
//     * 根据测试数据生成测试用例
//     * @param driver
//     * @param data
//     */
//    public static void generateTestCases(WebDriver driver, List<String[]> data) {
//        testCases = new ArrayList<BaseTestCase>();
//        List<TestStep> steps = new ArrayList<TestStep>();
//        // 每次循环必定产生一条测试步骤
//        String name = null;
//        String desc = null;
//        String module = null;
//        for (int i = 0; i < data.size(); i++) {
//            TestStep step;
//            String[] stepStringData = data.get(i);
//            // 判断是否是新测试用例
//            if (stepStringData[0] != null) {
//                if (!steps.isEmpty()) {
//                    // 每次循环都会检查步骤列表steps是否为空，如果非空而且用例编号存在，则表示出现一条新的用例
//                    // 此时需要把上一条用例保存到列表中，并清空steps
//                    //清空数据会导致引用该对象的列表也清空,因此此处使用浅拷贝复制一份对象
//                    List<TestStep> testSteps = (List<TestStep>) ((ArrayList<TestStep>) steps).clone();
//                    TestCaseFactory caseFactory = new TestCaseFactory(name, desc, module, testSteps);
//                    BaseTestCase testCase = caseFactory.buildTestCase();
//                    testCases.add(testCase);
//                    steps.clear();  //清空测试步骤列表
//                }
//
//                // 获取名称，描述，模块，为下个循环保存值
//                name = stepStringData[1];
//                desc = stepStringData[2];
//                module = stepStringData[3];
//            }
//
//            //产生一条测试步骤
//            TestStepFactory stepFactory = new TestStepFactory();
//            stepFactory.setActionType(stepStringData[4]);
//            stepFactory.setElementSearchType(stepStringData[5]);
//            stepFactory.setElementSearchValue(stepStringData[6]);
//            stepFactory.setEnterValue(stepStringData[7]);
//            stepFactory.setVerityKeyword(stepStringData[8]);
//            stepFactory.setVertiyElementSearchType(stepStringData[9]);
//            stepFactory.setVertiyElementSearchValue(stepStringData[10]);
//            step = stepFactory.buildStep(driver);
//            steps.add(step);
//
//            // 防止data最后一个元素数据没有添加进入testcases
//            if (i == data.size() - 1) {
//                TestCaseFactory caseFactory = new TestCaseFactory(name, desc, module, steps);
//                BaseTestCase testCase = caseFactory.buildTestCase();
//                testCases.add(testCase);
//            }
//
//        }
//        System.out.println(testCases);
//    }

    /**
     * 从excel表格获取数据
     *
     * @param filePath
     * @return
     * @throws IOException
     * @throws BiffException
     */
    public static List<String[]> generateTestData(String filePath) throws IOException, BiffException {
        File xlsFile = new File(filePath);
        // 获得工作簿对象
        Workbook workbook = Workbook.getWorkbook(xlsFile);
        // 获得所有工作表
        Sheet[] sheets = workbook.getSheets();
        List<String[]> data = null;
        // 遍历工作表
        if (sheets != null) {
            for (Sheet sheet : sheets) {
                // 获得行数
                int rowLenth = sheet.getRows();
                // 获得列数
                int colLenth = sheet.getColumns();

                assert rowLenth != 0;
                assert colLenth != 0;
                data = new ArrayList<String[]>();
                for (int rowNum = 1; rowNum < rowLenth; rowNum++) {
                    String[] rowValue = new String[11];
                    for (int colNum = 0; colNum < colLenth; colNum++) {
//                        String v = sheet.getCell(colNum, rowNum).getContents();
//                        if (v.equals("")) {
//                            rowValue[colNum] = null;
//                            continue;
//                        }
                        rowValue[colNum] = sheet.getCell(colNum, rowNum).getContents();
                    }
                    data.add(rowValue);
                }
            }
        }
        workbook.close();
        return data;
    }


    /**
     * 创建xls文档
     *
     * @param filePath
     * @throws IOException
     * @throws WriteException
     */
    public static void createXlsFile(String filePath) throws IOException, WriteException {
        File xlsFile = new File(filePath);
        // 创建一个工作簿
        WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
        // 创建一个工作表
        workbook.createSheet("sheet1", 0);
        workbook.write();
        workbook.close();
    }

    /**
     * 按照坐标写入值
     *
     * @param filePath
     * @param rowNumber
     * @param columNumber
     * @param value
     * @throws IOException
     * @throws WriteException
     */
    public static void write(String filePath, int rowNumber, int columNumber, String value) throws IOException, WriteException, BiffException {
        File xlsFile = new File(filePath);
        WritableWorkbook workbook;
        WritableSheet sheet;
        if (!xlsFile.exists()) {
            // 创建一个工作簿
            workbook = Workbook.createWorkbook(xlsFile);
            sheet = workbook.createSheet("sheet1", 0);
        } else {
            throw new IOException("xls file not exist!");
        }

        // 向工作表中添加数据
        sheet.addCell(new Label(columNumber, rowNumber, value));
        workbook.write();
        workbook.close();
    }


    /**
     * 读取并打印excel文件所有内容
     *
     * @param filePath
     * @throws IOException
     * @throws BiffException
     */
    public static void printContext(String filePath) throws IOException, BiffException {
        File xlsFile = new File(filePath);
        // 获得工作簿对象
        Workbook workbook = Workbook.getWorkbook(xlsFile);
        // 获得所有工作表
        Sheet[] sheets = workbook.getSheets();
        // 遍历工作表
        if (sheets != null) {
            for (Sheet sheet : sheets) {
                // 获得行数
                int rows = sheet.getRows();
                // 获得列数
                int cols = sheet.getColumns();
                // 读取数据
                for (int row = 0; row < rows; row++) {
                    for (int col = 0; col < cols; col++) {
                        if (sheet.getCell(col, row).getContents().equals("")) {
                            System.out.printf("%10s", "null");
                        }
                        System.out.printf("%10s", sheet.getCell(col, row)
                                .getContents());
                    }
                    System.out.println();
                }
            }
        }
        workbook.close();
    }
}
